#include "ResourceManager.h"


CResourceManager::CResourceManager(void)
{
}


CResourceManager::~CResourceManager(void)
{
}

SDL_Surface* LoadPNG( std::string filepath )
{
	//The image that's loaded
	SDL_Surface* loadedImage = NULL;

	//Load the image
	loadedImage = IMG_Load(filepath.c_str());

	if (loadedImage != NULL)
	{
		//logger << "Image loaded: " << filepath.c_str() << std::endl;
		return loadedImage;
	}

	else
	{
		//logger << "Image not loaded: " << filepath.c_str() << std::endl;
		return NULL;
	}
}

SDL_Surface* LoadBMP( std::string filepath )
{
	//The image that's loaded
	SDL_Surface* loadedImage = NULL;

	//The optimized image that will be used
	SDL_Surface* optimizedImage = NULL;

	//Load the image
	loadedImage = IMG_Load( filepath.c_str() );

	//If the image loaded
	if( loadedImage != NULL )
	{
		//Create an optimized image
		optimizedImage = SDL_DisplayFormat( loadedImage );

		//Free the old image
		SDL_FreeSurface( loadedImage );
	}

	//Return the optimized image
	return optimizedImage;
}

SDL_Surface* CResourceManager::LoadImage( std::string filepath )
{
	SDL_Surface* loadedImage = NULL;

	if (filepath.find(".bmp") != std::string::npos)
	{
		loadedImage = LoadBMP(filepath);
	}

	else if (filepath.find(".png") != std::string::npos)
	{
		loadedImage = LoadPNG(filepath);
	}

	else if (filepath.find(".jpg") != std::string::npos)
	{
		loadedImage = LoadPNG(filepath);
	}

	if (loadedImage != NULL)
	{
		for each (SDL_Surface* image in loadedImages)
		{
			if (loadedImage == image)
			{
				SDL_FreeSurface(loadedImage);
				return image;
			}
		}

		loadedImages.push_back(loadedImage);
		return loadedImage;
	}

	return NULL;
}